<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML>
<html>
  <head>
    <base href="<%=basePath%>">
    <title>role管理</title>
	<!-- 导入所有需要的css -->
	<jsp:include page="/common/head.jsp"></jsp:include>
  </head>
  <body>
<div class="container-fluid mainContainer">
<nav class="navbar navbar-default searchnav"  role="navigation">
      <div class="container-fluid">
        <div class="navbar-header ">
        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#searchFormDiv">
            <span class="sr-only">表单</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <b class="navbar-brand" style="margin-top: -3px;" >role管理</b>
        </div>
        <div class="collapse navbar-collapse" id="searchFormDiv">
          <form id="searchForm" class="navbar-form navbar-left" onkeydown="if(event.keyCode==13){return false;}" action="role/export" target="_self" method="post" role="search">
            <div class="form-group">
            <div class="input-group">
            <div class="input-group-addon">搜索</div>
              <input type="text" class="form-control" id="keywords" name="form.keywords" style="width:120px;"  placeholder="输入关键词">
            </div>
            </div>
		
            <button type="button" class="btn btn-primary" id="searchBtn"><i class="glyphicon glyphicon-search">查询</i></button>
          </form>
         	<div class="navbar-right">
	      		<button id="addBtn" type="button" class="btn btn-primary  navbar-btn" objId="0"><span class="glyphicon glyphicon-plus"></span> 添加</button>
	     	 </div>
        </div>
      </div>
    </nav>

  	<div class="row">
  		<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
  			<table id="roleTable" class="table table-striped table-bordered  table-hover" cellspacing="0" width="100%">
  				<thead>
							<tr>
								<th>序号</th>
								<th>角色名</th>
								<th>备注</th>
								<th style="width:160px;">操作</th>
							</tr>  
				<thead>
  			</table>
  		</div>
  	</div>
  	</div>
	<!-- 导入所有需要的Js -->
	<jsp:include page="/common/js.jsp"></jsp:include>
	<script type="text/javascript">
		$(function(){
			//初始化表格
			var datatable=$("#roleTable").DataTable({
		        "ajax": {
		            "url": "role/list",
		            "type": "POST",
		            "cache":false,
		            "data":function(data){
		            	//填充搜素条件数据
		            	fillSearchData(data,"searchForm");
		            }
		        },
		        "columns": [
		                    	{
									"searchable": false,
								    "orderable" : false,
								    "sClass": "tcenter",
								    "data" : "index",
								    "defaultContent": "",
								    "targets" : 0
								},
				            { "data": "name" ,"defaultContent": ""},
				            { "data": "remark","defaultContent": ""},
							{
				            	"searchable": false,
							    "orderable" : false,
							    "class":"tcenter",
								"render": function(data,type,row) {
									var btns=addEditButton({img:true,objId:row.id})+
											 addDelButton({img:true,objId:row.id})+
											 addOptButton({title:"菜单权限",objId:row.id,img:true,imgUrl:"assets/css/imgs/info.png",href:"sourceFolder?roleId="+row.id})+
											 addOptButton({title:"功能权限",objId:row.id,img:true,imgUrl:"assets/css/imgs/info.png",href:"template?roleId="+row.id});
									return  btns;
								}
							} 
				        ],
				        "order": [[ 3, 'desc' ]],
				        "fnRowCallback": function( nRow, aData, iDisplayIndex ) {
							$('td:eq(0)', nRow).html((datatable.page()*datatable.page.len())+iDisplayIndex+1);
							return nRow;
						}
			});
			//添加按钮  事件 弹出
			$("#addBtn").click(function(){
				var url="role/initAdd";
				openModal({
					id:"addDemo",
					title:"role添加",
					width:550,
					height:"auto",
					loadType:"ajax",
					url:url,
					footer:{
						close:{text:"取消"},
						submit:{text:"提交",event:function(){
							submitForm(function(data){
								closeWaittingForm("roleForm");
								if(data.success){
									$("#modal_addDemo").modal("hide");
									appSuccessMsg("添加成功", 1000);
									setTimeout(function(){
										datatable.draw( false );
									},800);
								}else{
									appErrorMsg(data.msg==null?data:data.msg, 2000);
								}
							});
							}}}	
				});
			 });
			//修改按钮 事件 弹出
			datatable.on("click",".editBtn",function(){
				var _this = $(this);
				var id=_this.attr("objId");
				var url="role/initModify?id="+id;
				openModal({
					id:"modifyDemo",
					title:"role修改",
					width:500,
					height:"auto",
					loadType:"ajax",
					url:url,
					footer:{
						close:{text:"取消"},
						submit:{text:"提交",event:function(){
							submitForm(function(data){
								closeWaittingForm("roleForm");
								if(data.success){
									$("#modal_modifyDemo").modal("hide");
									appSuccessMsg("修改成功", 1000);
									setTimeout(function(){
										datatable.draw( false );
									},800);
								}else{
									appErrorMsg(data.msg==null?data:data.msg, 2000);
								}
							});
							}}}	
				});
			 });
			//excel按钮 事件 弹出
			$("#addByExcelBtn").click(function(){
				var _this = $(this);
				var url="role/initAddByExcel";
				openModal({
					id:"addByExcelDemo",
					title:"用户信息-Excel导入",
					height:"auto",
					loadType:"ajax",
					url:url,
					footer:{
						close:{text:"取消"},
						submit:{text:"提交",event:function(){
							submitForm(function(data){
								closeWaittingForm("roleForm");
								if(data.success){
									$("#modal_addByExcelDemo").modal("hide");
									appSuccessMsg("导入成功", 1000);
									setTimeout(function(){
										datatable.draw( false );
									},800);
								}else{
									appErrorMsg(data.msg==null?data:data.msg, 2000);
								}
							});
							}}}	
				});
				
				
			 });
			
			//删除事件
			datatable.on("click",".delBtn",function () {
				if(confirm("确定删除?")){
					appLoading("正在删除,请稍后...");
					var _this = $(this);
					$.ajax({
						type:"POST",
						url:"role/delete",
						data: {id:_this.attr("objId")},
						dataType:"json",
						success:function(result){
							if(result.success){
								datatable.row('.selected').remove().draw( false );
								closeAppLoadingNow();
								appSuccessMsg("删除成功", 1000);
							}else{
								appErrorMsg(result.msg==null?result:result.msg,1000);
							}
						}
					});
				}else{
					closeAppLoadingNow();
				}
			});
			
			//生成代码事件
			datatable.on("click",".genAllBtn",function () {
				if(confirm("确定生成操作?")){
					appLoading("正在生成,请稍后...");
					var _this = $(this);
					$.ajax({
						type:"POST",
						url:"generate/generateAll",
						data: {roleId:_this.attr("objId")},
						dataType:"json",
						success:function(result){
							if(result.success){
								closeAppLoadingNow();
								appSuccessMsg("生成成功", 1000);
							}else{
								appErrorMsg(result.msg==null?result:result.msg,1000);
							}
						}
					});
				}else{
					closeAppLoadingNow();
				}
			});
			//生成代码事件
			datatable.on("click",".genCodeBtn",function () {
				if(confirm("确定生成操作?")){
					appLoading("正在生成,请稍后...");
					var _this = $(this);
					$.ajax({
						type:"POST",
						url:"generate/generateCode",
						data: {roleId:_this.attr("objId")},
						dataType:"json",
						success:function(result){
							if(result.success){
								closeAppLoadingNow();
								appSuccessMsg("生成成功", 1000);
							}else{
								appErrorMsg(result.msg==null?result:result.msg,1000);
							}
						}
					});
				}else{
					closeAppLoadingNow();
				}
			});
			//生成代码事件
			datatable.on("click",".genPageBtn",function () {
				if(confirm("确定生成操作?")){
					appLoading("正在生成,请稍后...");
					var _this = $(this);
					$.ajax({
						type:"POST",
						url:"generate/generatePage",
						data: {roleId:_this.attr("objId")},
						dataType:"json",
						success:function(result){
							if(result.success){
								closeAppLoadingNow();
								appSuccessMsg("生成成功", 1000);
							}else{
								appErrorMsg(result.msg==null?result:result.msg,1000);
							}
						}
					});
				}else{
					closeAppLoadingNow();
				}
			});
			//生成代码事件
			datatable.on("click",".genFileBtn",function () {
				if(confirm("确定生成操作?")){
					appLoading("正在生成,请稍后...");
					var _this = $(this);
					$.ajax({
						type:"POST",
						url:"generate/generateFile",
						data: {roleId:_this.attr("objId")},
						dataType:"json",
						success:function(result){
							if(result.success){
								closeAppLoadingNow();
								appSuccessMsg("生成成功", 1000);
							}else{
								appErrorMsg(result.msg==null?result:result.msg,1000);
							}
						}
					});
				}else{
					closeAppLoadingNow();
				}
			});
			
			//绑定导出按钮事件
			$("#exportBtn").click(function(){
				appLoading("正在导出，请稍后...",1000);
				$("#searchForm").submit();
			});
			//绑定搜索按钮事件
			$("#searchBtn").click(function(){
				appLoading("正在查询，请稍后...");
				datatable.draw(false);
			});
			//绑定刷新数据事件
			datatable.on("draw",function(){
				closeAppLoading();
			})
		});
	</script>
  </body>
</html>
